//精灵
const Events=require('events');
class Sprite extends Events{
    constructor(options={
        properties:{}, //需要监控改动的属性
    }){
        super();
        Object.defineProperty(this, 'propertiesChange', {
            value:{},
            writable:true
        }); //存储一段时间内的属性变动
        this.properties=!!options.properties?options.properties:{};
        let keys=Object.keys(this.properties);
        for(let i=0;i<keys.length;i++){
            Object.defineProperty(this, keys[i], {
                get: function() {
                    return this.properties[keys[i]];
                },
                set: function(value) {
                    this.properties[keys[i]] = this.propertiesChange[keys[i]] = value;
                    this.emit('properties',{key:keys[i],value:value});
                }
            });
        }
    }
    //获取改变的属性值，未改变的不在此列，获取后重新统计
    getPropertiesChange(){
        const r=this.propertiesChange;
        this.propertiesChange={};
        return r;
    }
}

module.exports=Sprite;